package com.example.knowman.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.knowman.ObService.OBDigitalArchMainService;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ReturnMessage.*;
import com.example.knowman.dao.ArchLabelMainSimple;
import com.example.knowman.dao.archLabelDetailEx;
import com.example.knowman.dao.knowLabelMainEx;
import com.example.knowman.dao.knowMainEx;
import com.example.knowman.entity.archLabelMain;
import com.example.knowman.entity.digitalArchMain;
import com.example.knowman.entity.knowLabelMain;
import com.example.knowman.entity.knowMain;
import com.example.knowman.service.IArchLabelMainService;
import com.example.knowman.service.IStaffListService;
import com.example.knowman.service.IarchLabelDetailService;
import com.example.knowman.service.IknowLabelMainService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//9.2标签
@RestController
@RequestMapping("/archLabelMain")
public class ArchLabelMainController {
    private final IArchLabelMainService iarchLabelMainService;

    private final OBKnowMainService obKnowMainService;

    private final OBDigitalArchMainService obDigitalArchMainService;

    private final IarchLabelDetailService iarchLabelDetailService;

    private final IStaffListService iStaffListService;

    public ArchLabelMainController(IArchLabelMainService iarchLabelMainService,OBKnowMainService obKnowMainService,OBDigitalArchMainService obDigitalArchMainService,
                                   IarchLabelDetailService iarchLabelDetailService,IStaffListService iStaffListService) {
        this.iarchLabelMainService = iarchLabelMainService;
        this.obKnowMainService = obKnowMainService;
        this.obDigitalArchMainService = obDigitalArchMainService;
        this.iarchLabelDetailService = iarchLabelDetailService;
        this.iStaffListService = iStaffListService;
    }



    //查询所有
    @GetMapping("getAll")
    public ResponseEntity<MessageExArchLabelMainSimple> getAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                               @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                               @RequestParam(required = false) String archTitle) {


        //获取所有archTitle符合的digitalArchMain
//        Map<Integer, knowMain> KMMap = obKnowMainService.getMapTitleLike(knowTitle);
        Map<Integer, digitalArchMain> DAM = obDigitalArchMainService.getMapTitleLike(archTitle);

        //获取所有archLabelMain
        List<archLabelMain> lis = iarchLabelMainService.getAll();

        //将在上面List中的archLabelMain加入ipage
        IPage<ArchLabelMainSimple> Respage = new Page<>(pageNum, pageSize);
        List<ArchLabelMainSimple> records = new ArrayList<>();
        List<ArchLabelMainSimple> recordsPage = new ArrayList<>();

        for(archLabelMain item:lis){
            if (DAM.get(item.getKnowId())!=null){
                ArchLabelMainSimple temp = new ArchLabelMainSimple();
                temp.setArchLblId(item.getArchLblId());
                temp.setNotes(item.getNotes());
                temp.setLblContent(item.getLblContent());
                temp.setKnowId(item.getKnowId());
                temp.setArchMainCode(DAM.get(item.getKnowId()).getArchMainCode());
                temp.setArchTitle(DAM.get(item.getKnowId()).getArchTitle());

                records.add(temp);
            }
        }
        int start = (pageNum - 1) * pageSize;
        int end = Math.min(start + pageSize, records.size());
        recordsPage = records.subList(start, end);

//
        Respage.setRecords(recordsPage);
        Respage.setTotal(records.size());


        MessageExArchLabelMainSimple mes = new MessageExArchLabelMainSimple();
        mes.setCode(200);
        mes.setTip("查询成功");
        mes.setList(Respage);


        return ResponseEntity.ok(mes);
    }

//
    @GetMapping("Update")
    public ResponseEntity<Message> update(@RequestParam Integer archLblId,
                                          @RequestParam(required = false) String lblContent,
                                          @RequestParam(required = false) String notes,
                                          @RequestParam(required = false) String staffCode) {

        //得到staffCode对应名字
        String userName = null;
        if(staffCode !=null && !staffCode.isEmpty()){
            Map<String, String> SID_Name = iStaffListService.getIdAndName();
            userName = SID_Name.get(staffCode);
        }


        Message res = iarchLabelMainService.updateByknowLblId(archLblId,lblContent,notes,userName);
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }


    //查
    @GetMapping("getSingle")
    public ResponseEntity<MExArchLabelMain> getSingle(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                      @RequestParam Integer archLblId) {

        archLabelMain res = iarchLabelMainService.getSingleByArchLblId(archLblId);
        IPage<archLabelDetailEx> lis = iarchLabelDetailService.getDetails(pageNum,pageSize,archLblId);

        //获取档案code
        String archTitle = null;
        Map<Integer, digitalArchMain> DAM = obDigitalArchMainService.getMapTitleLike(archTitle);


        MExArchLabelMain resMessage = new MExArchLabelMain();
        resMessage.setArchMainCode(DAM.get(res.getKnowId()).getArchMainCode());
        resMessage.setItem(res);
        resMessage.setList(lis);

        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        return ResponseEntity.ok(resMessage);

    }
//
    //增
    //弃用
    @GetMapping("Insert")
    public ResponseEntity<Message> Insert(@RequestParam Integer knowId,
                                          @RequestParam(required = false) String lblContent,
                                          @RequestParam String userName,
                                          @RequestParam(required = false) String notes) {




        Message res = iarchLabelMainService.Insert(knowId,lblContent,userName,notes);
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }
//
//
    //根据knowId返回可供选择的标签lists+所属类别
    //这个得修改了，传入知识类别的id返回对应标签们
    @GetMapping("getLabelsByKnowCateId")
    public ResponseEntity<MessageExKnowLabelMainBackLabels> getLabelsByKnowCateId(@RequestParam Integer knowCateId) {

        knowLabelMainEx res = iarchLabelMainService.getLabelsByknowCateId(knowCateId);
        MessageExKnowLabelMainBackLabels resMessage = new MessageExKnowLabelMainBackLabels();
        resMessage.setItem(res);
        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        return ResponseEntity.ok(resMessage);

    }
}
